<template>
	<view class="cu-modal bottom-modal" :class="modalName=='bottomModal'?'show':''" @click="hideModal">
	  <view class="cu-dialog">
	    <view class="my-address" v-if="addressArr2x[0].length>0">
	        <block v-for="(item,index) in addressArr2x" :key="index">
	            <swiper class="swiper" :vertical="true" :current="addressArr[index]" :display-multiple-items="5" @change="bindchange" :data-key="index">
	                <swiper-item class="item"></swiper-item>
	                <swiper-item class="item"></swiper-item>
	                <block v-for="(sonItem,sonIndex) in item" :key="sonIndex">
	                    <swiper-item class="item" :class="sonIndex==addressArr[index]?'cur':''">{{sonItem}}</swiper-item>
	                </block>
	                <swiper-item class="item"></swiper-item>
	                <swiper-item class="item"></swiper-item>
	            </swiper>
	        </block>
	        <view class="mask"></view>
	    </view>
	    <view v-else class="note">暂无地区数据</view>
	    <view class="cu-bar btn-group" style="padding: 0 40rpx 40rpx;">
	        <button class="cu-btn text-green line-green shadow" @click="hideModal">取消</button>
	        <button class="cu-btn bg-green shadow-blur" @click="submit">确定</button>
	    </view>
	  </view>
	</view>
</template>

<script>
	export default {
		name:"uni-location",
		/**
		 * 组件的属性列表
		 */
		props:{
			modalName:{
				type:String,
				value:''
			},
			addressJSON:{
				type:Object,
				value:{}
			},
		},
		data() {
			return {
				addressArr2x:[[],[],[]],
				addressArr:[0,0,0]
			};
		},
		updated() {
			this.init();
		},
		methods:{
			init(){
				console.log("初始化")
				const provinces = [];
				for (const key in this.addressJSON) {
					provinces.push(key);
				}
				this.addressArr2x[0] = provinces;
				this._showAddress();
			},
			hideModal(){
				this.$emit('hideModal',null)
			},
			submit(){
				this.$emit('handleLocation',this.addressArr)
			},
			bindchange(e){
				const key = e.currentTarget.dataset.key;
				const val = e.detail.current
				let addressArr = this.addressArr;
				if(key==0){
					addressArr = [val,0,0];
				}else if(key==1){
					addressArr[1] = val;
					addressArr[2] = 0;
				}else{
					addressArr[2] = val;
				}
				this.addressArr = addressArr
				this._showAddress();
			},
			    
			_showAddress(){
				const {addressJSON,addressArr} = this;
				const provinceName = Object.keys(addressJSON)[addressArr[0]];
				const citys = [];
				for (const key in addressJSON[provinceName]) {
					citys.push(key);
				}
				const cityName = citys[addressArr[1]];
				const areas = addressJSON[provinceName][cityName];
				
				this.addressArr2x[1] = citys;
				this.addressArr2x[2] = areas;
				
			},
		}
	}
</script>

<style lang="less">
.bg-green {
	background-color: #39b54a;
	color: #ffffff;
}


.line-green{
	color: #39b54a;
}
.line-green::after{
	border-color: #39b54a;
}

.shadow {
	box-shadow: 6rpx 6rpx 8rpx rgba(48, 156, 63, 0.2);
}

.shadow-blur {
	position: relative;
}

.shadow-blur::before {
	content: "";
	display: block;
	background: inherit;
	filter: blur(10rpx);
	position: absolute;
	width: 100%;
	height: 100%;
	top: 10rpx;
	left: 10rpx;
	z-index: -1;
	opacity: 0.4;
	transform-origin: 0 0;
	border-radius: inherit;
	transform: scale(1, 1);
}


/* ==================
          按钮
 ==================== */

 .cu-btn {
	position: relative;
	border: 0rpx;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	box-sizing: border-box;
	padding: 0 30rpx;
	font-size: 28rpx;
	height: 64rpx;
	line-height: 1;
	text-align: center;
	text-decoration: none;
	overflow: visible;
	margin-left: initial;
	transform: translate(0rpx, 0rpx);
	margin-right: initial;
}

.cu-btn::after {
	display: none;
}

.cu-btn:not([class*="bg-"]) {
	background-color: #f0f0f0;
}

.cu-btn.shadow-blur::before {
	top: 4rpx;
	left: 4rpx;
	filter: blur(6rpx);
	opacity: 0.6;
}

.cu-btn.button-hover {
	transform: translate(1rpx, 1rpx);
}

/* ==================
          操作条
 ==================== */

 .cu-bar {
	display: flex;
	position: relative;
	align-items: center;
	min-height: 100rpx;
	justify-content: space-between;
}

.cu-bar.btn-group {
	justify-content: space-around;
}

.cu-bar.btn-group .cu-btn {
	padding: 20rpx 32rpx;
}

.cu-bar.btn-group .cu-btn {
	flex: 1;
	margin: 0 20rpx;
	max-width: 50%;
}

/* ==================
         模态窗口
 ==================== */

 .cu-modal {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	z-index: 1110;
	opacity: 0;
	outline: 0;
	text-align: center;
	-ms-transform: scale(1.185);
	transform: scale(1.185);
	backface-visibility: hidden;
	perspective: 2000rpx;
	background: rgba(0, 0, 0, 0.6);
	transition: all 0.3s ease-in-out 0s;
	pointer-events: none;
}

.cu-modal::before {
	content: "\200B";
	display: inline-block;
	height: 100%;
	vertical-align: middle;
}

.cu-modal.show {
	opacity: 1;
	transition-duration: 0.3s;
	-ms-transform: scale(1);
	transform: scale(1);
	overflow-x: hidden;
	overflow-y: auto;
	pointer-events: auto;
}

.cu-dialog {
	position: relative;
	display: inline-block;
	vertical-align: middle;
	margin-left: auto;
	margin-right: auto;
	width: 680rpx;
	max-width: 100%;
	background-color: #f8f8f8;
	border-radius: 10rpx;
	overflow: hidden;
}

.cu-modal.bottom-modal::before {
	vertical-align: bottom;
}

.cu-modal.bottom-modal .cu-dialog {
	width: 100%;
	border-radius: 0;
}

.cu-modal.bottom-modal {
	margin-bottom: -1000rpx;
}

.cu-modal.bottom-modal.show {
	margin-bottom: 0;
}


.cu-modal .cu-dialog>.cu-bar:first-child .action{
  min-width: 100rpx;
  margin-right: 0;
  min-height: 100rpx;
}

.my-address{
    height: 600rpx;
    padding:0rpx 40rpx 0rpx;
    margin-bottom: 20rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}
.my-address .swiper{
    height: 100%;
    flex:1;
    position: relative;
    z-index: 200;
}
.my-address .swiper .item{
    display: flex;
    justify-content: center;
    align-items: center;
    color: #999999;
}
.my-address .swiper .item.cur{
    color: #333333;
}
.my-address .mask{
    position: absolute;
    top: 40%;
    left: 0;
    z-index: 100;
    width: 100%;
    height: 20%;
    border: 5rpx solid #e9e9e9;
}
.note{
    padding: 40rpx 0 100rpx;
    color: #999999;
}

</style>